home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Mac / Lib / py_resource.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-06-23  |  4.0 KB  |  83 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. '''Creation of PYC resources'''
  5. import os
  6. import Res
  7. import __builtin__
  8. READ = 1
  9. WRITE = 2
  10. smAllScripts = -3
  11.  
  12. def Pstring(str):
  13.     '''Return a pascal-style string from a python-style string'''
  14.     if len(str) > 255:
  15.         raise ValueError, 'String too large'
  16.     
  17.     return chr(len(str)) + str
  18.  
  19.  
  20. def create(dst, creator = 'Pyth'):
  21.     '''Create output file. Return handle and first id to use.'''
  22.     
  23.     try:
  24.         os.unlink(dst)
  25.     except os.error:
  26.         pass
  27.  
  28.     Res.FSpCreateResFile(dst, creator, 'rsrc', smAllScripts)
  29.     return open(dst)
  30.  
  31.  
  32. def open(dst):
  33.     output = Res.FSpOpenResFile(dst, WRITE)
  34.     Res.UseResFile(output)
  35.     return output
  36.  
  37.  
  38. def writemodule(name, id, data, type = 'PYC ', preload = 0, ispackage = 0):
  39.     '''Write pyc code to a PYC resource with given name and id.'''
  40.     if ispackage:
  41.         data = data[:4] + '\xff\x00\x00\x00' + data[8:]
  42.     else:
  43.         data = data[:4] + '\x00\x00\x00\x00' + data[8:]
  44.     res = Res.Resource(data)
  45.     res.AddResource(type, id, name)
  46.     if preload:
  47.         attrs = res.GetResAttrs()
  48.         attrs = attrs | 4
  49.         res.SetResAttrs(attrs)
  50.     
  51.     res.WriteResource()
  52.     res.ReleaseResource()
  53.  
  54.  
  55. def frompycfile(file, name = None, preload = 0, ispackage = 0):
  56.     '''Copy one pyc file to the open resource file'''
  57.     if name == None:
  58.         (d, name) = os.path.split(file)
  59.         name = name[:-4]
  60.     
  61.     id = findfreeid()
  62.     data = __builtin__.open(file, 'rb').read()
  63.     writemodule(name, id, data, preload = preload, ispackage = ispackage)
  64.     return (id, name)
  65.  
  66.  
  67. def frompyfile(file, name = None, preload = 0, ispackage = 0):
  68.     '''Compile python source file to pyc file and add to resource file'''
  69.     import py_compile
  70.     py_compile.compile(file)
  71.     file = file + 'c'
  72.     return frompycfile(file, name, preload = preload, ispackage = ispackage)
  73.  
  74. _firstfreeid = None
  75.  
  76. def findfreeid(type = 'PYC '):
  77.     '''Find next free id-number for given resource type'''
  78.     global _firstfreeid, _firstfreeid
  79.     id = _firstfreeid
  80.     _firstfreeid = _firstfreeid + 1
  81.     return id
  82.  
  83.